package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Goal;
import com.kaishengit.util.db.DBHelp;
import com.kaishengit.util.db.RowMapper;

public class GoalDao {
	
	private DBHelp<Goal> db = new DBHelp<Goal>();
	
	public void save (Goal goal){
		String sql = "insert into t_goal (id,title,description,projectId,createtime,state) values(?,?,?,?,?,?)";
		db.executeSQL(sql, goal.getId(),goal.getTitle(),goal.getDescription(),goal.getProjectId(),goal.getCreatetime(),goal.getState());
	}
	public void update(Goal goal){
		String sql="update t_goal set title=?,description=?,projectId=?,createtime=?,state=? where id=?";
		db.executeSQL(sql, goal.getTitle(),goal.getDescription(),goal.getProjectId(),goal.getCreatetime(),goal.getState(),goal.getId());
	}
	
	public List<Goal> findByProjectId(int projectId){
		String sql="select id,title,description,projectId,createtime,state from t_goal where projectId=?";
		return db.executeQueryToList(sql, new GoalRowMapper(), projectId);
	}
	
	public Goal findGoalById(int id) {
		String sql = "select id,title,description,projectId,createtime,state from t_goal where id=? ";
		return db.executeQueryToObject(sql, new GoalRowMapper(), id);
	}
	
	
	private class GoalRowMapper implements RowMapper<Goal>{
		public Goal mapRow(ResultSet rs) throws SQLException {
			Goal goal = new Goal();
			goal.setId(rs.getInt("id"));
			goal.setTitle(rs.getString("title"));
			goal.setDescription(rs.getString("description"));
			goal.setProjectId(rs.getInt("projectId"));	
			goal.setCreatetime(rs.getString("createtime"));
			goal.setState(rs.getInt("state"));
			return goal;
		}
	}
}
